package com.yygh.cmn.control;

import com.yygh.cmn.service.DictService;
import com.yygh.common.result.Result;
import com.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(description = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;


    @GetMapping("findChildData/{id}")
    @ApiOperation(value = "根据数据id查询子数据列表")
    public Result findChildData(@PathVariable Long id){
        List<Dict> chlidData = dictService.findChlidData(id);
        return Result.ok(chlidData);
    }
    @ApiOperation(value = "导出数据字典")
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response){
        dictService.exportDictData(response);
    }

    @ApiOperation(value = "导入数据字典")
    @PostMapping("importData")
    //MultipartFile 这个类一般是用来接受前台传过来的文件，
    public Result importData(MultipartFile file){
        dictService.importDictData(file);
        return Result.ok();
    }

    //根据dictcode和value查询
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode, @PathVariable String value){
        String dictName = dictService.getDictName(dictCode, value);
        return dictName;
    }

    //value查询
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value){
        String dictName = dictService.getDictName("",value);
        return dictName;
    }

    //根据dictCode获取下级节点
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("findByDictCode/{dictcode}")
    public Result findByDictCode(@PathVariable String dictcode){
        List<Dict> childDcode = dictService.findChildDcode(dictcode);
        return Result.ok(childDcode);
    }


}
